import QtQuick 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"

Flipable {
     width: 1200
     height: 700
     id: flipable
     property bool hideable : false
     //property bool listVisible : true
     anchors.centerIn: parent
     
    
     Timer {
	 function startTimer () {
	    stop();
	    if(!flipable.hideable){
	      console.log("timer disabled");
	      return;
	    }
	    console.log("timer enabled===> start");
	    start();
	 }
	 id:hideTimer
         interval: 5000
         running: false
         repeat: false
         onTriggered: {
	   //hide
	   appControl.hide();
	 }
     }
     
    DbusService {
	id : videoGene
	service:'org.kdeadmouse.apps.videoplayer'
	path:'/generic/videoplayer'
	registerName : 'gene_videoplayer'
    }
    
    DbusService {
	id:serv
	
	signal started()
	signal allFinished(string videoType)
	signal keyEvent(int key,string videoType)
	
	service:'org.kdeadmouse.apps.videoplayer'
	path:'/specific/videoplayer'
	registerName : 'videoplayer'
	
	onStarted : {
	    console.log('strated');
	    flipable.hideable=true;
	    
	}
	
	onKeyEvent : {
	  if(videoType!='allocine' && videoType!='magikfile'){
	      return;
	  }
	  console.log('keyEvent:' + key);
	  appControl.show();
	}
	
	onAllFinished : {
	    flipable.hideable=false;
	    if(videoType!='allocine' && videoType!='magikfile'){
	      return;
	    }
	    appControl.show();
	  }
	  
	onInitialized : {
	   console.log('serv completed');
	   videoplayer.keyEvent.connect(keyEvent);
	   videoplayer.started.connect(started);
	   videoplayer.allFinished.connect(allFinished);
	   console.log('connect 2');
	} 
	
	Component.onCompleted: {
	  checkServiceExists();
	}
    }
     
     
      Loader {
	  id:listLoader
	  source: "listeFilms.qml"
	  focus: true //is set to true for the Loader as well as the Item in the dynamically loaded 
	  onLoaded : {
	    console.log('liste loaded');
	    flipable.front = listLoader.item;
	  }
      }
      

      Loader {
	  id:detLoader
	  source: "detailMovie.qml"
	  //focus: true //is set to true for the Loader as well as the Item in the dynamically loaded 
	  onLoaded : {
	    console.log('detail loaded');
	    flipable.back = detLoader.item;
	  }
      }
	      
      Connections {
	  target: detLoader.item
	  onReturnToList : {
	    flipable.state='front';
	  }
	  onShowVideo : {
	    console.log('showing:'+videourl);
	    serv.invokeDbusMethod('loadNowVideo',['magikfile',videourl]);
	    videoGene.invokeDbusMethod('show',[]);
	  }
	  onShowTrailer : {
	    console.log('showing trailer:'+trailerCode);
	    serv.invokeDbusMethod('loadNowVideo',['allocine',trailerCode]);
	    videoGene.invokeDbusMethod('show',[]);
	  }
      }
      
      Connections {
	  target: listLoader.item
	  onShowDetail : {
	    var objSelected = listLoader.item.getModelSelected();
	    detLoader.item.infos=objSelected;
	    flipable.state='back'
	  }
      }
     


     transform: Rotation {
         id: rotation
         origin.x: flipable.width/2
         origin.y: flipable.height/2
         axis.x: 0; axis.y: 1; axis.z: 0     // set axis.y to 1 to rotate around y-axis
         angle: 0    // the default angle
     }

     states: [State {
	  name: "back"
	  PropertyChanges { target: rotation; angle: 180 }
	  //PropertyChanges { target: detLoader.item;focus: true }
	  PropertyChanges { target: flipable.front;focus: false }
	  PropertyChanges { target: flipable.back;focus: true }
	  //PropertyChanges { target: listLoader;focus: false }
	  //when: !listVisible
	},
	State {
	  name: "front"
	  PropertyChanges { target: rotation; angle: 0 }
	  //PropertyChanges { target: listLoader.item;focus: true }
	  PropertyChanges { target: flipable.back;focus: false }
	  PropertyChanges { target: flipable.front;focus: true }
	  //PropertyChanges { target: detLoader;focus: false }
	   //when: listVisible
	}]
	
     transitions: Transition {
         NumberAnimation { target: rotation; property: "angle"; duration: 500 }
     }

     MouseArea {
         anchors.fill: parent
         onClicked: {
	   if(flipable.state=='back')
	     flipable.state='front'
	   else
	     flipable.state='back';
	}
     }
    
     

     Component.onCompleted: {
	state='front';
     }
     
     Keys.onPressed: {
	 hideTimer.startTimer();
	 if (event.key == Qt.Key_Backspace) {
             console.log("Key_Backspace");
             event.accepted = true;
	     appControl.hide();
         }
     }
     
     Keys.onEscapePressed : {
       appControl.hide();
     }
 }